﻿Imports System
Imports System.Windows
Imports libDAL

Namespace libModClassVB.Classes
    Public Class clsGRNGenerator : Implements IDisposable
        Private _DAL As cls_DAL

        Public WriteOnly Property set_DAL
            Set(value)
                _DAL = value
            End Set
        End Property

        Private Structure Queries

            Public Function get_Querymfdefnofor(ByVal strCode As String) As String
                Return "Select * from mfdefnofor where code= '" & strCode & "'"
            End Function

            Public Function get_Querymfgrndefno(ByVal strPortID As String) As String
                Return "Select * from mfgrndefno where portid = '" & strPortID & "'"
            End Function

            Public Function get_QueryUpdatemfgrndefno(ByVal strRunNo As String, ByVal strPortID As String, ByVal strRundate As String) As String
                Dim strGRNRefNo As String = String.Empty

                strGRNRefNo = "UPDATE mfgrndefno set runno = '" & strRunNo & "', " & _
                                     "rundate = '" & strRundate & "' " & _
                              "WHERE portid = '" & strPortID & "'"

                Return strGRNRefNo
            End Function

        End Structure

        Private Query As Queries

        Private Function get_mfdefnofor(ByVal strCode As String) As DataRow()
            Dim drRecords() As DataRow = {}
            Try
                drRecords = _DAL.get_TableDetail(Me.Query.get_Querymfdefnofor(strCode)).Select()
            Catch ex As Exception
                MsgBox(ex.Message + " get_mfdefnofor")
            End Try
            Return drRecords
        End Function

        Private Function get_mfgrndefno(ByVal strPortID As String) As DataRow()
            Dim drRecords() As DataRow = {}
            Try
                drRecords = _DAL.get_TableDetail(Me.Query.get_Querymfgrndefno(strPortID)).Select()
            Catch ex As Exception
                MsgBox(ex.Message + " get_mfgrndefno")
            End Try
            Return drRecords
        End Function


        Public Function GetGRNNo(ByVal code As String, ByVal portid As String) As String
            Dim StrCode As String
            Dim strGRNNo = String.Empty
            Try

                Dim str1 As String
                Dim str2 As String
                Dim str3 As String
                Dim str4 As String
                Dim str5 As String
                Dim str6 As String
                Dim str7 As String
                Dim str8 As String
                Dim str9 As String

                Dim drrs As DataRow() = _DAL.get_TableDetail(Query.get_Querymfdefnofor(code)).Select() 'cnMain.Execute("Select * from mfdefnofor where code='" & clsGenFunction.get_ConvertQ(code) & "'")
                If drrs.Length > 0 Then
                    Dim drRsNo As DataRow() = _DAL.get_TableDetail(Query.get_Querymfgrndefno(portid)).Select()    '.Open("Select * from mfgrndefno where portid='" & clsGenFunction.get_ConvertQ(portid) & "'", cnMain, adOpenDynamic, adLockOptimistic)
                    If drRsNo.Length > 0 Then
                        If (clsGenFunction.get_checkStringNull(drrs(0).IsNull("p1")) = "" And clsGenFunction.get_checkStringNull(drrs(0).Item("p2")) = "" _
                            And clsGenFunction.get_checkStringNull(drrs(0).Item("p3")) = "" And clsGenFunction.get_checkStringNull(drrs(0).Item("p4")) = "" _
                            And clsGenFunction.get_checkStringNull(drrs(0).Item("p5")) = "" And clsGenFunction.get_checkStringNull(drrs(0).Item("p6")) = "" _
                            And clsGenFunction.get_checkStringNull(drrs(0).Item("p7")) = "" And clsGenFunction.get_checkStringNull(drrs(0).Item("p8")) = "" _
                            And clsGenFunction.get_checkStringNull(drrs(0).Item("p9")) = "") _
                            Or (drrs(0).IsNull("p1") And drrs(0).IsNull("p2") And drrs(0).IsNull("p3")) And drrs(0).IsNull("p4") And drrs(0).IsNull("p5") And drrs(0).IsNull("p6") _
                            And drrs(0).IsNull("p7") And drrs(0).IsNull("p8") And drrs(0).IsNull("p9") Then
                            MsgBox("Pls Setup Document Type Numbering!", , "Message")
                            strGRNNo = "0000"
                            Exit Function
                        Else
                            If clsGenFunction.get_checkStringNull(drrs(0).Item("p1")) = "d" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p2")) = "d" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p3")) = "d" Or _
                                clsGenFunction.get_checkStringNull(drrs(0).Item("p4")) = "d" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p5")) = "d" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p6")) = "d" Or _
                                clsGenFunction.get_checkStringNull(drrs(0).Item("p7")) = "d" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p8")) = "d" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p9")) = "d" Then
                                If Format(drRsNo(0).Item("rundate"), "yyyy-MM-dd") <> Format(clsGenFunction.get_ServerDateTimeRAW(_DAL), "yyyy-MM-dd") Then
                                    _DAL.set_GenQueryExNonQuery(Query.get_QueryUpdatemfgrndefno("1", portid, Format(clsGenFunction.get_ServerDateTimeRAW(_DAL), "yyyy-MM-dd")))
                                End If

                            ElseIf (clsGenFunction.get_checkStringNull(drrs(0).Item("p1")) = "m" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p2")) = "m" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p3")) = "m" Or _
                                    clsGenFunction.get_checkStringNull(drrs(0).Item("p4")) = "m" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p5")) = "m" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p6")) = "m" Or _
                                    clsGenFunction.get_checkStringNull(drrs(0).Item("p7")) = "m" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p8")) = "m" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p9")) = "m") Then
                                If Month(Format(drRsNo(0).Item("rundate"), "yyyy-MM-dd")) <> Month(Format(clsGenFunction.get_ServerDateTimeRAW(_DAL), "yyyy-MM-dd")) Then
                                    _DAL.set_GenQueryExNonQuery(Query.get_QueryUpdatemfgrndefno("1", portid, Format(clsGenFunction.get_ServerDateTimeRAW(_DAL), "yyyy-MM-dd")))
                                End If

                            ElseIf (clsGenFunction.get_checkStringNull(drrs(0).Item("p1")) = "y" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p2")) = "y" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p3")) = "y" Or _
                                    clsGenFunction.get_checkStringNull(drrs(0).Item("p4")) = "y" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p5")) = "y" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p6")) = "y" Or _
                                    clsGenFunction.get_checkStringNull(drrs(0).Item("p7")) = "y" Or clsGenFunction.get_checkStringNull(drrs(0).Item("p8")) = "y" Or _
                                    clsGenFunction.get_checkStringNull(drrs(0).Item("p9")) = "y") Then
                                If Year(Format(drRsNo(0).Item("rundate"), "yyyy-MM-dd")) <> Year(Format(clsGenFunction.get_ServerDateTimeRAW(_DAL), "yyyy-MM-dd")) Then
                                    _DAL.set_GenQueryExNonQuery(Query.get_QueryUpdatemfgrndefno("1", portid, Format(clsGenFunction.get_ServerDateTimeRAW(_DAL), "yyyy-MM-dd")))
                                End If
                            ElseIf (drrs(0).Item("p1") = "t" Or drrs(0).Item("p2") = "t" Or drrs(0).Item("p3") = "t" Or drrs(0).Item("p4") = "t" Or
                                    drrs(0).Item("p5") = "t" Or drrs(0).Item("p6") = "t" Or drrs(0).Item("p7") = "t" Or drrs(0).Item("p8") = "t" Or drrs(0).Item("p9") = "t") Then
                                If Year(Format(drRsNo(0).Item("rundate"), "yyyy-MM-dd")) <> Year(Format(clsGenFunction.get_ServerDateTimeRAW(_DAL), "yyyy-MM-dd")) Then
                                    _DAL.set_GenQueryExNonQuery(Query.get_QueryUpdatemfgrndefno("1", portid, Format(clsGenFunction.get_ServerDateTimeRAW(_DAL), "yyyy-MM-dd")))
                                End If
                            End If
                            str1 = GRNNoFormat(drRsNo, clsGenFunction.get_checkStringNull(drrs(0).Item("p1")))
                            str2 = GRNNoFormat(drRsNo, clsGenFunction.get_checkStringNull(drrs(0).Item("p2")))
                            str3 = GRNNoFormat(drRsNo, clsGenFunction.get_checkStringNull(drrs(0).Item("p3")))
                            str4 = GRNNoFormat(drRsNo, clsGenFunction.get_checkStringNull(drrs(0).Item("p4")))
                            str5 = GRNNoFormat(drRsNo, clsGenFunction.get_checkStringNull(drrs(0).Item("p5")))
                            str6 = GRNNoFormat(drRsNo, clsGenFunction.get_checkStringNull(drrs(0).Item("p6")))
                            str7 = GRNNoFormat(drRsNo, clsGenFunction.get_checkStringNull(drrs(0).Item("p7")))
                            str8 = GRNNoFormat(drRsNo, clsGenFunction.get_checkStringNull(drrs(0).Item("p8")))
                            str9 = GRNNoFormat(drRsNo, clsGenFunction.get_checkStringNull(drrs(0).Item("p9")))
                            StrCode = str1 & str2 & str3 & str4 & str5 & str6 & str7 & str8 & str9

                            Dim strRunNo As String = clsGenFunction.get_checkComputeNullInt(drRsNo(0).Item("runno")) + 1

                            _DAL.set_GenQueryExNonQuery(Query.get_QueryUpdatemfgrndefno(strRunNo, portid, clsGenFunction.get_ServerDateTimeRAW(_DAL).ToString("yyyy-MM-dd")))

                            strGRNNo = StrCode
                        End If
                    Else
                        MsgBox("Pls Setup Document Type Numbering!", , "Message")
                        strGRNNo = "0000"
                    End If

                Else
                    MsgBox("Pls Setup Document Type Numbering!", , "Message")
                    strGRNNo = "0000"
                End If
            Catch ex As Exception
                MsgBox(ex.Message + " GetGRNNo")
            End Try
            Return strGRNNo
        End Function

        Public Function GRNNoFormat(ByVal drRSNo As DataRow(), code As String) As String
            Dim no As Integer
            Dim Width As Integer
            Dim zcode As String = String.Empty
            Dim strGRNNoFormat = String.Empty
            Try
                Select Case code
                    Case "a"
                        strGRNNoFormat = IIf(drRSNo(0).IsNull("portid"), "", Trim(drRSNo(0).Item("portid")))
                    Case "b"
                        strGRNNoFormat = IIf(drRSNo(0).IsNull("pre1"), "", Trim(drRSNo(0).Item("pre1")))
                    Case "c"
                        strGRNNoFormat = IIf(drRSNo(0).IsNull("pre2"), "", Trim(drRSNo(0).Item("pre2")))
                    Case "d"
                        strGRNNoFormat = clsGenFunction.get_ServerDateTimeRAW(_DAL).ToString("dd")
                    Case "m"
                        strGRNNoFormat = clsGenFunction.get_ServerDateTimeRAW(_DAL).ToString("MM")
                    Case "y"
                        strGRNNoFormat = clsGenFunction.get_ServerDateTimeRAW(_DAL).ToString("yy")
                    Case "t"
                        strGRNNoFormat = clsGenFunction.get_ServerDateTimeRAW(_DAL).ToString("yyyy")
                    Case "s"
                        strGRNNoFormat = IIf(drRSNo(0).IsNull("separate"), "", Trim(drRSNo(0).Item("separate")))
                    Case "r"
                        no = Len(Trim(Str((IIf(drRSNo(0).IsNull("runno"), 0, drRSNo(0).Item("runno"))))))
                        Width = IIf(drRSNo(0).IsNull("runwid"), 1, drRSNo(0).Item("runwid")) - no
                        For i = 1 To Width
                            zcode = zcode + "0"
                        Next
                        strGRNNoFormat = zcode & Trim(Str(IIf(drRSNo(0).IsNull("runno"), 0, drRSNo(0).Item("runno"))))
                    Case Else
                        strGRNNoFormat = ""
                End Select
            Catch ex As Exception
                MsgBox(ex.Message + " GRNNoFormat")
            End Try
            Return strGRNNoFormat
        End Function


#Region "IDisposable Support"
        Private disposedValue As Boolean ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: dispose managed state (managed objects).
                End If

                ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
                ' TODO: set large fields to null.
            End If
            Me.disposedValue = True
        End Sub

        ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
        'Protected Overrides Sub Finalize()
        '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        '    Dispose(False)
        '    MyBase.Finalize()
        'End Sub

        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class
End Namespace
